/*
 * @Author: your name
 * @Date: 2020-12-30 15:37:17
 * @LastEditTime: 2021-02-18 13:30:32
 * @LastEditors: Please set LastEditors
 * @Description:操作数据库
 * @FilePath: \node-server\service\user.js
 */
const { querySql } = require('../db')
module.exports = {
    /**
  * @description: 获取用户信息
  * @param {*} username
  * @return {*}
  */
    async getUserInfoDao(username) {
        const sql = `select username,id,create_time from users where username='${username}'`
        return await querySql(sql)
    },
    /**
     * @description: 登录逻辑校验
     * @param {string} username
     * @param {string} password
     * @return {*}
     */
    async checkLoginDao(username, password) {
        let userIsRegister = true //用户是否注册
        const sql = `select password from users where username='${username}'`
        const { results } = await querySql(sql)
        if (results.length) {
            if (results[0].password == password) {
                return {
                    status: 1,
                    userIsRegister,
                }
            } else {
                return {
                    status: 0,
                    userIsRegister,
                }
            }
        } else {
            userIsRegister = false
            return {
                status: 0,
                userIsRegister,
            }
        }
    },
    /**
     * @description: 用户注册
     * @param {string} username
     * @param {string} password
     * @return {*}
     */
    async registerDao(username, password, create_time) {
        const { status } = await module.exports.userIsExist(username)
        if (status) {
            const sql = `insert into users (username, password , create_time) values ('${username}', '${password}' , '${create_time}')`
            return querySql(sql)
        } else {
            return status
        }

    },
    // 查询用户是否存在
    async userIsExist(username) {
        const sql = `select * from users where username='${username}'`
        const { results } = await querySql(sql)
        if (results.length) {
            return {
                status: 0,
                msg: '用户已存在'
            }
        } else {
            return {
                status: 1,
                msg: '用户未注册'
            }
        }
    }
}